package com.kxmall.web.controller.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.common.exception.ServiceException;
import com.kxmall.product.domain.KxStoreCategory;
import com.kxmall.product.mapper.KxStoreCategoryMapper;
import com.kxmall.web.controller.coupon.service.IKxStoreCouponService;
import com.kxmall.product.domain.KxStoreProduct;
import com.kxmall.product.mapper.KxStoreProductMapper;
import com.kxmall.coupon.domain.KxStoreCoupon;
import com.kxmall.coupon.domain.bo.KxStoreCouponBo;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.StringUtils;
import com.kxmall.coupon.domain.vo.KxStoreCouponVo;
import com.kxmall.coupon.mapper.KxStoreCouponMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * 优惠券Service业务层处理
 *
 * @author kxmall
 * @date 2023-02-17
 */
@RequiredArgsConstructor
@Service
public class KxStoreCouponServiceImpl implements IKxStoreCouponService {

    private final KxStoreCouponMapper baseMapper;

    private final KxStoreCategoryMapper categoryMapper;



    /**
     * 查询优惠券
     */
    @Override
    public KxStoreCouponVo queryById(Long id) {
        KxStoreCouponVo wcv = baseMapper.selectVoById(id);
        return wcv;
    }

    /**
     * 查询优惠券列表
     */
    @Override
    public TableDataInfo<KxStoreCouponVo> queryPageList(KxStoreCouponBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KxStoreCoupon> lqw = buildQueryWrapper(bo);
        Page<KxStoreCouponVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询优惠券列表
     */
    @Override
    public List<KxStoreCouponVo> queryList(KxStoreCouponBo bo) {
        LambdaQueryWrapper<KxStoreCoupon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KxStoreCoupon> buildQueryWrapper(KxStoreCouponBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KxStoreCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), KxStoreCoupon::getTitle, bo.getTitle());
        lqw.eq(bo.getIntegral() != null, KxStoreCoupon::getIntegral, bo.getIntegral());
        lqw.eq(bo.getCouponPrice() != null, KxStoreCoupon::getCouponPrice, bo.getCouponPrice());
        lqw.eq(bo.getUseMinPrice() != null, KxStoreCoupon::getUseMinPrice, bo.getUseMinPrice());
        lqw.eq(bo.getCouponTime() != null, KxStoreCoupon::getCouponTime, bo.getCouponTime());
        lqw.eq(bo.getSort() != null, KxStoreCoupon::getSort, bo.getSort());
        lqw.eq(bo.getStatus() != null, KxStoreCoupon::getStatus, bo.getStatus());
        lqw.eq(bo.getType() != null, KxStoreCoupon::getType, bo.getType());
        lqw.eq(bo.getIsDel() != null, KxStoreCoupon::getIsDel, bo.getIsDel());
        return lqw;
    }

    /**
     * 新增优惠券
     */
    @Override
    public Boolean insertByBo(KxStoreCouponBo bo) {
        KxStoreCoupon add = BeanUtil.toBean(bo, KxStoreCoupon.class);
        validEntityBeforeSave(add);

        //需要选择二级分类
        Long count = categoryMapper.selectCount(new LambdaQueryWrapper<KxStoreCategory>().ne(KxStoreCategory::getPid, 0L).eq(KxStoreCategory::getId, bo.getCategoryId()));
        if (count == 0L) {
            throw new ServiceException("请选择二级分类");
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改优惠券
     */
    @Override
    public Boolean updateByBo(KxStoreCouponBo bo) {
        KxStoreCoupon update = BeanUtil.toBean(bo, KxStoreCoupon.class);
        validEntityBeforeSave(update);
        //需要选择二级分类
        Long count = categoryMapper.selectCount(new LambdaQueryWrapper<KxStoreCategory>().ne(KxStoreCategory::getPid, 0L).eq(KxStoreCategory::getId, bo.getCategoryId()));
        if (count == 0L) {
            throw new ServiceException("请选择二级分类");
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxStoreCoupon entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除优惠券
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Map<String, Object> queryAllForSelect(KxStoreCouponBo bo) {
        List<KxStoreCouponVo> wmStoreCouponVos = queryList(bo);
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", wmStoreCouponVos);
        map.put("totalElements", wmStoreCouponVos.size());
        return map;
    }
}
